#include "ffmpegThreadProxy.h"
#include <iostream>
#include <unistd.h>

bool FfmpegThreadProxy::start(VideoOption &option) {
    this->video_option = &option;
    this->thread.start([&]() { this->run(); });
    this->monitor_thread.start([&]() { this->monitor(); });
    return true;
}
void FfmpegThreadProxy::stop() {
    this->monitor_thread.stop();
    this->thread.stop();
}

void FfmpegThreadProxy::run() {
    FILE *pp = popen(this->video_option->get_ffmepg_cmd(), "r"); // build pipe
    if (!pp) {
        return;
    }

    char tmp[1024] = {0};
    while (fgets(tmp, sizeof(tmp), pp) != NULL && !this->thread.want_exit()) {
        std::cout << tmp << std::endl; // can join each line as string
    }
    pclose(pp);
}

void FfmpegThreadProxy::monitor() {
    while (!this->monitor_thread.want_exit()) {
        sleep(1);
    }
}